﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
namespace Kingdee.Bos.ProjectName.BillExecuteSql
{
    [Description("【表单插件】价格调整单：物料、价目表值更新，添加调整前单价")]
    [HotUpdate]
    public class JiaGeTiaoZheng : AbstractBillPlugIn
    {

        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);

            if (e.Field.Key.Equals("F_VHTP_WLMC", StringComparison.OrdinalIgnoreCase))
            {

                BindNeedAuditOrderInfo(e.Row);

            }

        }
        private void BindNeedAuditOrderInfo(int asd)
        {
            //获取单据体物料更新的值 物料ID
            DynamicObjectCollection FEntity = this.View.Model.DataObject["FEntity"] as DynamicObjectCollection;
            string WuLiaoID = FEntity[asd]["F_VHTP_WLMC_ID"].ToString();

            
            //根据物料ID 获取调整前价格 和 价目表ID
            String SQLSelectJG1 = "select T1.FID ,FPRICE  " +
                "from T_SAL_PRICELISTENTRY T1 LEFT JOIN T_SAL_PRICELIST T2 ON T2.FID=T1.FID " +
                "where   FMATERIALID='" + WuLiaoID + "' and  F_UIXC_DECIMAL>0  and t1.FFORBIDSTATUS='A'" +
                "order by F_UIXC_DECIMAL asc";
            DataTable dtJG1;
            DynamicObjectCollection DyObjs = DBServiceHelper.ExecuteDynamicObject(this.Context, SQLSelectJG1);
            dtJG1 = DBUtils.ExecuteDataSet(this.Context, SQLSelectJG1).Tables[0];

            if (dtJG1.Rows.Count > 0)
            {
                int count = dtJG1.Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    //价目表 调整前价格
                    String JMID = dtJG1.Rows[i][0].ToString();
                    String JG1 = dtJG1.Rows[i][1].ToString();

                    //this.View.Model.SetItemValueByNumber("F_VHTP_DJ1", JG1, asd);
                    // 获取单据体数据包          
                    DynamicObjectCollection Entrys = this.View.Model.DataObject["FEntity"] as DynamicObjectCollection;
                    if (DyObjs != null && DyObjs.Count > 0)
                    {
                        //获取选中行
                        int index = this.View.Model.GetEntryCurrentRowIndex("FEntity");
                        DynamicObject entry = Entrys[index] as DynamicObject;
                        if (i == 0)
                        {
                            entry["F_VHTP_JM1_Id"] = JMID;
                            entry["F_VHTP_DJ11"] = JG1;
                        }
                        else if (i == 1)
                        {
                            entry["F_VHTP_JM2_Id"] = JMID;
                            entry["F_VHTP_DJ21"] = JG1;
                        }
                        else if (i == 2)
                        {
                            entry["F_VHTP_JM3_Id"] = JMID;
                            entry["F_VHTP_DJ31"] = JG1;
                        }
                        else if (i == 3)
                        {
                            entry["F_VHTP_JM4_Id"] = JMID;
                            entry["F_VHTP_DJ41"] = JG1;
                        }
                        else if (i == 4)
                        {
                            entry["F_VHTP_JM5_Id"] = JMID;
                            entry["F_VHTP_DJ51"] = JG1;
                        }
                        else if (i == 5)
                        {
                            entry["F_VHTP_JM6_Id"] = JMID;
                            entry["F_VHTP_DJ61"] = JG1;
                        }
                        else if (i == 6)
                        {
                            entry["F_VHTP_JM7_Id"] = JMID;
                            entry["F_VHTP_DJ71"] = JG1;
                        }
                        else if (i == 7)
                        {
                            entry["F_VHTP_JM8_Id"] = JMID;
                            entry["F_VHTP_DJ81"] = JG1;
                        }
                        else if (i == 8)
                        {
                            entry["F_VHTP_JM9_Id"] = JMID;
                            entry["F_VHTP_DJ91"] = JG1;
                        }
                        else if (i == 9)
                        {
                            entry["F_VHTP_JM10_Id"] = JMID;
                            entry["F_VHTP_DJ101"] = JG1;
                        }
                        //else
                        //{
                        //    //警告
                        //    this.View.ShowWarnningMessage
                        //        ("超出可加载价目表数量",
                        //        action: (result) =>
                        //        {
                        //        });
                        //}


                        //添加到单据体数据包
                        Entrys[index] = entry;
                    }
                    // 重新加载数据包              
                    DBServiceHelper.LoadReferenceObject(this.Context, Entrys.ToArray(), Entrys.FirstOrDefault().DynamicObjectType, false);
                    this.View.UpdateView("FEntity");
                }
               
            }
            else
            {
                //警告
                this.View.ShowWarnningMessage
                    ("该物料未添加到任何价目表",
                    action: (result) =>
                    {
                            //关闭页面
                            //this.View.Close();
                        }
                    );
            }

        }





    }
}
